package com.gjszn.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class HttpUtils {
	
	public final static String HQ_URL = "http://218.95.142.169:16956/hqweb/hq/hqV_gnnt.jsp";
	
	/*
	宁运油10T,OIL10,3143.00,3144.00,3157.00,3161.00,3143.00,[3145.00],0,0,3144.00,0,0,2.00,3151.00,3145.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2015-10-28 10:32:06
	宁运精矿粉10T,NYGP10,9045.00,9048.00,9050.00,9062.00,9039.00,9061.00,0,0,9048.00,0,0,16.00,9068.00,9061.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2015-10-28 10:32:13
	宁运矿粉10T,NYSP10,16199.00,16204.00,16187.00,16259.00,16158.00,16247.00,0,0,16204.00,0,0,48.00,16272.00,16247.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2015-10-28 10:32:31
	宁运现货铜10T,NYCU10,33068.00,33058.00,33049.00,33061.00,32944.00,32985.00,0,0,33058.00,0,0,-83.00,33025.00,32985.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2015-10-28 10:32:26
	宁运银10KG,NYAG10,3240.00,3241.00,3237.00,3252.00,3232.00,[3249].00,0,0,3241.00,0,0,9.00,3254.00,3249.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2015-10-28 10:32:31
	*/
	public static void main(String[] args) throws Exception {
//		List<String> strs = splitByLine(doGetJson(HQ_URL));
//		for(String s : strs){
//			System.out.println(s);
//		}
		String str = choosingItemsByItemKey(doGetJson(HQ_URL), "宁运油10T");
		System.out.println(toJson(str));
	}
	
	/**
	 * 通过get请求方式将获取到的内容转换成json对象
	 * @param url
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws Exception JSONObject
	 */
	public static String doGetJson(String url) throws IOException {
		String receStr = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// 创建一个get请求对象
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				receStr = EntityUtils.toString(entity, "UTF-8").trim();
			}
		} finally {
			if (response != null){
				response.close();
			}
			if (httpClient != null){
				httpClient.close();
			}
		}
		return receStr;
	}
	
	public static List<String> splitByLine(String receStr){
		String[] strs = receStr.split("\n");
		List<String> strList = new ArrayList<String>();
		for(String str : strs){
			if(str.contains("10T") || str.contains("10KG")){
				strList.add(str);
			}
		}
		return strList;
	}
	
	public static String choosingItemsByItemKey(String receStr, String itemKey){
		String[] strs = receStr.split("\n");
		for(String str : strs){
			if(str.contains(itemKey)){
				return str;
			}
		}
		return null;
	}
	
	public static JSONObject toJson(String str){
		JSONObject jsonObject = new JSONObject();
		String[] strs = str.split(",");
		jsonObject.put("name", strs[0]);
		jsonObject.put("price", (int)Float.parseFloat(strs[7]));
		jsonObject.put("time", strs[strs.length-1]);
		return jsonObject;
	}
	
}
